package com.chonor.mall.controller;

import com.chonor.mall.common.CommonPage;
import com.chonor.mall.common.CommonResult;
import com.chonor.mall.dto.CouponParam;
import com.chonor.mall.model.Coupon;
import com.chonor.mall.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券管理Controller
 */
@Controller
@Api(tags = "CouponController", description = "优惠券管理")
@RequestMapping("/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;
    
    @ApiOperation("添加优惠券")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody Coupon coupon) {
        int count = couponService.create(coupon);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除优惠券")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = couponService.delete(id);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改优惠券")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody Coupon coupon) {
        int count = couponService.update(id,coupon);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("根据优惠券名称和类型分页获取优惠券列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Coupon>> list(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "type",required = false) Integer type,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<Coupon> couponList = couponService.list(name,type,pageSize,pageNum);
        return CommonResult.success(CommonPage.restPage(couponList));
    }

    @ApiOperation("获取单个优惠券的详细信息")
    @RequestMapping(value = "/getItem/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CouponParam> getItem(@PathVariable Long id) {
        CouponParam couponParam = couponService.getItem(id);
        return CommonResult.success(couponParam);
    }

    @ApiOperation("获取单个优惠券的详细信息")
    @RequestMapping(value = "/setProduct/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult setProduct(@PathVariable Long id, List<Long> ids) {
        int count = couponService.setProduct(id, ids);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取单个优惠券的详细信息")
    @RequestMapping(value = "/setProductCategory/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult setProductCategory(@PathVariable Long id, List<Long> ids) {
        int count = couponService.setProductCategory(id, ids);
        if(count>0){
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
